package com.tbit.main.service.impl;

import cn.hutool.core.util.StrUtil;
import com.tbit.main.constant.MapConstant;
import com.tbit.main.dao.core.AccountUserWorkDao;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.AccountUserWork;
import com.tbit.main.remote.map.TXHttpClient;
import com.tbit.main.service.AccountUserWorkLogService;
import com.tbit.main.service.AccountUserWorkService;
import com.tbit.main.service.CalibrateService;
import com.tbit.main.service.RedisService;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author LMY
 * @create 2021-03-22 15:35
 */
@Service("accountUserWorkService")
public class AccountUserWorkServiceImpl implements AccountUserWorkService {

    @Autowired
    private AccountUserWorkDao accountUserWorkDao;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private AccountUserWorkLogService accountUserWorkLogService;
    @Autowired
    private RedisService redisService;

    @Override
    public AccountUserWork getByAccountUserId(Integer accountUserId) {
        return accountUserWorkDao.getByAccountUserId(accountUserId);
    }

    @Override
    public void insert(AccountUserWork accountUserWork) {
        accountUserWorkDao.insert(accountUserWork);
    }

    @Override
    public void update(AccountUserWork accountUserWork) {
        accountUserWorkDao.update(accountUserWork);
    }

    @Override
    public void delete(Integer accountUserId) {
        accountUserWorkDao.delete(accountUserId);
    }

    @Override
    @Transactional(value = "coreTransactionManager")
    public void work(AccountUser accountUserToken, Integer accountId, Integer mapType, Double lon, Double lat) {

        boolean locked = redisService.setNx("workClockIn_" + accountUserToken.getAccountUserId(), StrUtil.EMPTY, 3L);
        if (!locked) {
            throw new BaseException("正在打卡中,请稍后");
        }

        AccountUserWork accountUserWorkDb = getByAccountUserId(accountUserToken.getAccountUserId());

        String dateTime = DateTimeUtil.getNowTime();
        if (accountUserWorkDb == null) {
            AccountUserWork accountUserWork = new AccountUserWork();
            accountUserWork.setAccountUserId(accountUserToken.getAccountUserId());
            accountUserWork.setPhone(accountUserToken.getPhone());
            accountUserWork.setAccountId(accountId);
            accountUserWork.setStartTime(dateTime);
            accountUserWork.setStartLat(lat);
            accountUserWork.setStartLon(lon);
            accountUserWork.setStartAddress(TXHttpClient.getAddress(lon, lat));
            calibrateService.calTerUserWork(accountUserWork, mapType, MapConstant.MAP_ORI);
            insert(accountUserWork);
        } else {
            accountUserWorkDb.setEndTime(dateTime);
            Integer time = DateTimeUtil.getTime(accountUserWorkDb.getStartTime(), dateTime);
            accountUserWorkDb.setWorkTime(time);
            accountUserWorkDb.setEndLon(lon);
            accountUserWorkDb.setEndLat(lat);
            accountUserWorkDb.setEndAddress(TXHttpClient.getAddress(lon, lat));
            delete(accountUserWorkDb.getAccountUserId());
            calibrateService.calTerUserWork(accountUserWorkDb, mapType, MapConstant.MAP_ORI);
            accountUserWorkLogService.insert(accountUserWorkDb);
        }
    }

    @Override
    public List<AccountUserWork> getList() {
        return accountUserWorkDao.selectList();
    }
}
